import { parse } from 'url';

let userTableListDataSource = [];
let roleTableListDataSource = [];
for (let i = 0; i < 5; i += 1) {
  userTableListDataSource.push({
    key: i + 1,
    no: `${i + 1}`,
    username: `user${i + 1}`,
    mobile: '13888888888',
    roles: ['admin', 'user'],
    createTime: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
    status: true,
  });
}
for (let i = 0; i < 5; i += 1) {
  roleTableListDataSource.push({
    key: i + 1,
    roleId: `${i + 1}`,
    roleName: `USER${i + 1}`,
    roleCode: `ROLE_USER${i + 1}`,
    roleDesc: `用户_${i + 1}`,
    createTime: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
    updateTime: new Date(`2017-08-${Math.floor(i / 2) + 1}`),
  });
}

export function fakeUsers(req, res, u) {
  let path = u;
  if (!path || Object.prototype.toString.call(path) !== '[object String]') {
    path = req.url;
  }

  const params = parse(path, true).query;

  const dataSource = [...userTableListDataSource];

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function addUsers(req, res, u) {
  const roleId = userTableListDataSource[userTableListDataSource.length - 1].key;
  userTableListDataSource.push({
    key: roleId + 1,
    no: `${roleId + 1}`,
    username: req.body.username,
    mobile: req.body.mobile,
    roles: [req.body.roles],
    createTime: new Date(),
    status: true,
  });

  const dataSource = [...userTableListDataSource];

  const pageSize = 10;

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function updUsers(req, res, u) {
  userTableListDataSource = userTableListDataSource.map(data => {
    if (data.key === req.body.key) {
      return {
        ...data,
        username: req.body.username,
        mobile: req.body.mobile,
        roles: [req.body.roles],
        status: true,
      };
    } else {
      return data;
    }
  });

  const dataSource = [...userTableListDataSource];

  const pageSize = 10;

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function delUsers(req, res,) {
  userTableListDataSource = userTableListDataSource.filter(data => data.key !== req.body.key);
  const dataSource = [...userTableListDataSource];

  const pageSize = 10;

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }

  return result;
}

export function fakeRoles(req, res, u) {
  let path = u;
  if (!path || Object.prototype.toString.call(path) !== '[object String]') {
    path = req.url;
  }

  const params = parse(path, true).query;

  const dataSource = [...roleTableListDataSource];

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function addRoles(req, res,) {
  const roleId = roleTableListDataSource[roleTableListDataSource.length - 1].key;
  roleTableListDataSource.push({
    key: roleId + 1,
    roleId: `${roleId + 1}`,
    roleName: req.body.roleName,
    roleCode: req.body.roleCode,
    roleDesc: req.body.roleDesc,
    createTime: new Date(),
    updateTime: new Date(),
  });

  const dataSource = [...roleTableListDataSource];

  const pageSize = 10;

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function updRoles(req, res,) {
  roleTableListDataSource = roleTableListDataSource.map(data => {
    if (data.key === req.body.key) {
      return {
        ...data,
        roleName: req.body.roleName,
        roleCode: req.body.roleCode,
        roleDesc: req.body.roleDesc,
      };
    } else {
      return data;
    }
  });

  const dataSource = [...roleTableListDataSource];

  const pageSize = 10;

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function delRoles(req, res,) {
  roleTableListDataSource = roleTableListDataSource.filter(data => data.key !== req.body.key);
  const dataSource = [...roleTableListDataSource];

  const pageSize = 10;

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }

  return result;
}

export default {
  fakeUsers,
  addUsers,
  updUsers,
  delUsers,
  fakeRoles,
  addRoles,
  updRoles,
  delRoles,
};
